package course.service.impl;

import course.dao.TeacherDao;
import course.dao.impl.TeacherDaoImpl;
import course.pojo.entity.Teacher;
import course.pojo.vo.Page;
import course.service.TeacherService;
import course.utils.JDBCUtil;

import java.sql.Connection;
import java.util.List;

/**
 * @author HLF
 * @date 2022/6/30 21:07
 */
public class TeacherServiceImpl implements TeacherService {
    private TeacherDao teacherDao = new TeacherDaoImpl();

    @Override
    public Teacher getById(Integer tid) {
        Connection connection = JDBCUtil.getConnection();
        Teacher teacher = teacherDao.getById(tid, connection);
        JDBCUtil.closeConnection(connection);
        return teacher;
    }

    @Override
    public int addTeacher(Teacher teacher) {
        Connection connection = JDBCUtil.getConnection();
        int i = teacherDao.addTeacher(teacher, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int updateById(Teacher teacher) {
        Connection connection = JDBCUtil.getConnection();
        int i = teacherDao.updateById(teacher, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int updateInfo(Teacher teacher) {
        Connection connection = JDBCUtil.getConnection();
        int i = teacherDao.updateInfo(teacher, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int deleteById(Integer tid) {
        Connection connection = JDBCUtil.getConnection();
        int i = teacherDao.deleteById(tid, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public List<Teacher> getList() {
        Connection connection = JDBCUtil.getConnection();
        List<Teacher> list = teacherDao.getList(connection);
        JDBCUtil.closeConnection(connection);
        return list;
    }

    /**/

    @Override
    public int deleteSelect(List<Integer> ids) {
        Connection connection = JDBCUtil.getConnection();
        int i = teacherDao.deleteSelect(ids, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public long count() {
        Connection connection = JDBCUtil.getConnection();
        long count = teacherDao.count(connection);
        JDBCUtil.closeConnection(connection);
        return count;
    }

    /**
     * 分页
     *
     * @param pageNo 当前页码数
     * @return 返回Page对象
     */
    @Override
    public Page<Teacher> page(Integer pageNo) {
        Connection connection = JDBCUtil.getConnection();
        Page<Teacher> page = teacherDao.page(pageNo, connection);
        JDBCUtil.closeConnection(connection);
        return page;
    }

    /**
     * 模糊查询
     */
    @Override
    public Page<Teacher> query(String cname, Integer pageNo) {
        Connection connection = JDBCUtil.getConnection();
        Page<Teacher> page = teacherDao.query(cname, pageNo, connection);
        JDBCUtil.closeConnection(connection);
        return page;
    }

    @Override
    public List<Teacher> selectBathByIds(List<Integer> ids) {
        Connection connection = JDBCUtil.getConnection();
        List<Teacher> teachers = teacherDao.selectBathByIds(ids, connection);
        JDBCUtil.closeConnection(connection);
        return teachers;
    }
}
